<?php

class AuthorityController extends Controller
{
    public function init() {
        parent::init();
        $this->R->module = "权限管理";
    }
	public function actionIndex()
	{
		$this->render('index');
	}

    //获取角色列表及其权限
    public function actionList()
    {
        $arr = array();
        $page = $this->getParam('page');
        $rows = $this->getParam('rows');
        $criteria = new CDbCriteria();
        $criteria->order = '`ID` ASC';
        $count = Role::model()->count($criteria);
        $criteria->limit = $rows;
        $criteria->offset = ($page - 1) * $rows;
        $arr['total'] = $count;
        $models = Role::model()->findAll($criteria);
        $modules = Module::model()->FindAll();
        $modules_array = array();
        foreach ($modules as $ms)
        {
            $modules_array[$ms->ID] = $ms->Name;
        }
        foreach ($models as $m)
        {
            $brr = array();
            $authoritylist= array();

            foreach ($m->rolemodules as $mr)
            {
                $authoritylist[] = $modules_array[$mr->ModuleID];
            }
            $brr = $m->attributes;
            $brr['AuthorityList'] = $authoritylist;
            
            $arr['rows'][] = $brr;
        }
               
        echo json_encode($arr);exit;
    }
    //获取角色列表以tree的形式展示
    public function actionTreelist()
    {
        $models = Role::model()->findAll();
        $arr = array();
        $arr[0]['id'] = 0;
        $arr[0]['text'] = '全部';
        $arr[0]['iconCls'] = 'role_all';
        foreach ($models as $m)
        {
            $crr = array();
            $crr["id"] = $m->ID;
            $crr["text"] = $m->Name;
            $crr["iconCls"] = 'role_tree';
            $arr[0]['children'][] = $crr;
        }
        echo json_encode($arr);exit;
    }
    //获取角色列表以下拉菜单的形式展示
    public function actionCombolist()
    {
        $models = Role::model()->findAll();
        $arr = array();
        foreach ($models as $m)
        {
            $crr = array();
            $crr["id"] = $m->ID;
            $crr["text"] = $m->Name;
            $arr[] = $crr;
        }
        echo json_encode($arr);exit;
        
    }
    //根据ID获取角色权限信息
    public function actionListbyid()
    {
        $roleid = $this->getParam('id');
        $role = RoleModule::model()->findALl('RoleID =' . $roleid);

        $has_role = array();
        foreach ($role as $r)
        {   
           $has_role[] = $r->modules->ID;
        }
        $modules = Module::model()->findAll();
        $arr = array();
        foreach ($modules as $m)
        {
            $crr = array();
            $crr['text'] = $m->Name;
            $crr['id'] = $m->ID;
            $crr["iconCls"] = $m->Icon;
            if (in_array($m->ID,$has_role))
            {
                $crr['checked'] = true;
            }
            $arr[] = $crr;
        }
        echo json_encode($arr);exit;
    }
    //添加或编辑角色
    public function actionCreate()
    {
        $id = $this->getParam('id');
        $name = $this->getParam('Name');
        $modules = $this->getParam('modules');
        $modules = explode(',',$modules);
        $transaction = RoleModule::model()->dbConnection->beginTransaction();
        try  {
            if ($id)
            {
                $this->R->action = '修改角色';
                RoleModule::model()->deleteAll('RoleID =' . $id);
            }
            else
            {
                $this->R->action = '创建角色';
                $role = new Role;
                $role->Name = $name;
                $role->save();
                $id = $role->ID;
            }
            if ($modules)
            {
                foreach ($modules as $m)
                {
                    $module = new RoleModule;
                    $module->RoleID = (int)$id;
                    $module->ModuleID = (int)$m;
                    $module->save();
                }
            }
            $transaction->commit();
            $this->R->success = true;   
        }
        catch (Exception $e)
        {
            $transaction->rollBack();
            $this->R->success = false;  
        }
        $this->R->flush();
    }
    //删除角色
    public function actionDelete(){
        $this->R->action = '删除角色';
        $id = (int)$this->getParam('id');
        $result_role = Role::model()->deleteByPk($id);
        $result_rolemodule = RoleModule::model()->deleteAll("RoleID = :id", array(":id" => $id));
        if ($result_role) {
            $this->R->success = true;
            $this->R->msg = '操作成功';
        }
        else
        {
            $this->R->success = false;
            $this->R->msg = '操作失败';
        }
        $this->R->flush();
    }

}